<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="layui/css/layui.css" media="all" rel="stylesheet">
    <script src="layui/layui.js" type="text/javascript"></script>
    <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">
    <div class="layui-container" style="width:800px;height: 330px;padding-top: 60px;">
        <form action="" class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="gard layui-form-label">试卷名：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="name"
                           placeholder="请输入试卷名称"
                           type="text">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">开始时间：</label>
                <div class="layui-input-inline">
                    <input class="layui-input" id="beginTime" lay-verify="required" name="beginTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           type="text">
                </div>
                <label class="layui-form-label">结束时间：</label>
                <div class="layui-input-inline">
                    <input class="layui-input" id="endTime" lay-verify="required" name="endTime"
                           placeholder="yyyy-MM-dd HH:mm:ss"
                           type="text">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">单选题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="single"
                           placeholder="请输入题目数量"
                           type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="singleScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">多选题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="multiple"
                           placeholder="请输入题目数量"
                           type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="multipleScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">填空题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="fill"
                           placeholder="请输入题目数量"
                           type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="fillScore"
                           placeholder="请输入每题分值"
                           type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">判断题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="judge"
                           placeholder="请输入题目数量"
                           type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="judgeScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">简答题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveOne"
                           placeholder="请输入题目数量" type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveOneScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">计算题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveTwo"
                           placeholder="请输入题目数量" type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveTwoScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="gard layui-form-label">应用题：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveThree"
                           placeholder="请输入题目数量" type="number">
                </div>
                <label class="layui-form-label">每题分值：</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="subjectiveThreeScore"
                           placeholder="请输入每题分值" type="number">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="creat" lay-submit style="margin-left: 160px;">添加</button>
            </div>
        </form>

    </div>
</div>
<script charset="utf-8" src="layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        //日期时间选择器
        laydate.render({
            elem: '#beginTime'
            , type: 'datetime'
        });
        //日期时间选择器
        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
        });
        form.on('submit(creat)', function (data) {
            var res = data.field;
            console.log(res);
            if (new Date(res.endTime).getTime() - new Date(res.beginTime).getTime() <= 0) {
                alert("考试时间有误，请检查");
                return false;
            }
            if (data.field.single < 0 || data.field.multiple < 0 || data.field.fill < 0 || data.field.judge < 0 || data.field.subjectiveOne < 0 || data.field.subjectiveTwo < 0 || data.field.subjectiveThree < 0) {
                alert("题目数量不得小于零");
                return false;
            }
            if (data.field.singleScore <= 0 || data.field.multipleScore <= 0 || data.field.fillScore <= 0 || data.field.judgeScore <= 0 || data.field.subjectiveOneScore <= 0 || data.field.subjectiveTwoScore <= 0 || data.field.subjectiveThreeScore <= 0) {
                alert("题目分值必须大于零");
                return false;
            }
            res["course"] = sessionStorage.getItem("course");
            res["teacher"] = sessionStorage.getItem("id");
            $.ajax({
                url: '/question/makeTest/plan', //请求url
                data: JSON.stringify(res), //请求参数，类似于username=xxx&password=xxx
                type: 'post', //请求方式
                dataType: 'json', //返回值的类型
                contentType: 'application/json;charset=utf-8',
                success: function (result) {  //服务器成功返回时执行的函数
                    console.log(JSON.stringify(result));
                    if (result.code == 2000) {
                        alert("组卷成功，将跳转到试卷列表");
                        window.location.href = "question_add.html";
                    } else {
                        alert("添加失败!\n" + result.msg);
                    }
                }
            });

            return false;
        });
    });
</script>
</body>
</html>